package com.easychat.redis;

import com.easychat.entity.constants.RedisConstants;
import com.easychat.entity.dto.SysSettingDto;
import com.easychat.entity.dto.TokenUserInfoDto;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author Dongdong
 * @注释
 */
@Component("redisComponent")
public class RedisComponent {
    @Resource
    private RedisCache redisCache;

    //获取心跳
    public Long getUserHeartBeat(String userId) {
        return (Long) redisCache.getCacheObject(RedisConstants.REDIS_KEY_WS_HEART_BEAT + userId);
    }

    //设置心跳
    public void saveHeartBeat(String userId) {
        // key: userId  value：System.currentTimeMillis()
        redisCache.setCacheObject(RedisConstants.REDIS_KEY_WS_HEART_BEAT + userId, System.currentTimeMillis(), RedisConstants.REDIS_KEY_EXPIRE_HEART_BEAT, TimeUnit.MINUTES);
    }

    //删除心跳
    public void removeHeartBeat(String userId) {
        redisCache.deleteObject(RedisConstants.REDIS_KEY_WS_HEART_BEAT + userId);
    }

    //set token
    public void setTokenUserInfoDto(TokenUserInfoDto tokenUserInfoDto) {
        // key:token  value :tokenUserInfoDto
        redisCache.setCacheObject(RedisConstants.REDIS_KEY_WS_TOKEN + tokenUserInfoDto.getToken(), tokenUserInfoDto, RedisConstants.REDIS_EXPIRE_1DAY * 2, TimeUnit.MINUTES);
        //key:userid value:token
        redisCache.setCacheObject(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + tokenUserInfoDto.getUserId(), tokenUserInfoDto.getToken(), RedisConstants.REDIS_EXPIRE_1DAY * 2, TimeUnit.MINUTES);
    }

    //get token
    public TokenUserInfoDto getTokenUserInfoDto(String token) {
        TokenUserInfoDto tokenUserInfoDto = (TokenUserInfoDto) redisCache.getCacheObject(RedisConstants.REDIS_KEY_WS_TOKEN + token);
        return tokenUserInfoDto;
    }

    //get token by userid
    public TokenUserInfoDto getTokenUserInfoDtoByUserid(String userId) {
        String token = (String) redisCache.getCacheObject(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userId);
        return getTokenUserInfoDto(token);

    }

    //清理token根据userid
    public void cleanUserTokenByUserId(String userId) {
        String token = (String) redisCache.getCacheObject(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userId);

        redisCache.deleteObject(RedisConstants.REDIS_KEY_WS_TOKEN + token);//删除tokenUserInfoDto
        redisCache.deleteObject(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userId);//删除token
    }

    //获取系统设置
    public SysSettingDto getSysSettingDto() {
        SysSettingDto sysSettingDto = (SysSettingDto) redisCache.getCacheObject(RedisConstants.REDIS_KEY_SYS_SETTING);
        return sysSettingDto == null ? new SysSettingDto() : sysSettingDto;
    }

    //保存/修改 系统设置
    public void saveSysSettingDto(SysSettingDto sysSettingDto) {
        redisCache.setCacheObject(RedisConstants.REDIS_KEY_SYS_SETTING, sysSettingDto);
    }

    // 批量添加联系人：保存用户的联系人列表
    public void addUserContactBatch(String userId, List<String> contactIdList) {
        redisCache.setCacheList(RedisConstants.REDIS_KEY_USER_CONTACT + userId, contactIdList);
        redisCache.expire(RedisConstants.REDIS_KEY_USER_CONTACT + userId, RedisConstants.REDIS_EXPIRE_1DAY * 2);
    }

    //单独添加联系人：保存单个联系人
    public void addUserContact(String userId, String contactId) {
        List<String> userContactList = getUserContactList(userId);
        if (userContactList.contains(contactId)) {
            //已经存在该联系人
            return;
        }
        redisCache.lpush(RedisConstants.REDIS_KEY_USER_CONTACT + userId, contactId, RedisConstants.REDIS_EXPIRE_1DAY * 2);

    }
    public void removeUserContact(String userId,String contactId){
        redisCache.remove(RedisConstants.REDIS_KEY_USER_CONTACT + userId,contactId);
    }

    // 获取用户的联系人列表
    public List<String> getUserContactList(String userId) {
        return redisCache.getCacheList(RedisConstants.REDIS_KEY_USER_CONTACT + userId);
    }

    // 清空联系人
    public void deleteUserContact(String userId) {
        redisCache.deleteObject(RedisConstants.REDIS_KEY_USER_CONTACT + userId);
    }


}
